/*********************************************
 * Student    : Teodoro Cipresso
 * Course     : CS286
 * Professor  : Vidya Setlur
 * Assignment : Term Project (BlueTanks)
 *********************************************/
package edu.sjsu.cs286.teodoro.cipresso.sprites;

import java.io.IOException;

import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 * This class implements static methods for getting the graphic images used in
 * the game. The methods cache the result of a previous get so that cycles
 * aren't wasted reading from "disk" every time an image needs to be painted.
 * 
 * @author Teodoro Cipresso
 */
public class BlueTankImageHolder implements IBlueTankSprite {

	// Tank selection screen images
	private static Image selectBlue, selectRed, selectGreen, selectYellow;

	// Tank playfield images
	private static Image blueTank, redTank, greenTank, yellowTank;

	// Destroyed tank playfield images
	private static Image destroyedTankFrameOne, destroyedTankFrameTwo,
			destroyedTankFrameThree;

	// Tank firing playfied images
	private static Image fireBlue, fireRed, fireGreen, fireYellow;

	// Tank projectile image
	private static Image cannonBall;

	// Screen background image
	private static Image camouflage;

	// Dimensions of previous painting of screen background image
	private static int camouflageWidth, camouflageHeight;

	// Player win/lose graphic
	private static Image battleWon, battleLost, battleDraw;

	/**
	 * @param tankName,
	 *            the tank name constant from IBlueTankSprite
	 * @return the rgb of the tank represented by the name constant
	 */
	public static int getTankRgbByName(String tankName) {
		int color = 0;
		//
		if (tankName.equals(BLUE_TANK_NAME)) {
			color = BLUE_TANK_COLOR;
		} else if (tankName.equals(GREEN_TANK_NAME)) {
			color = GREEN_TANK_COLOR;
		} else if (tankName.equals(YELLOW_TANK_NAME)) {
			color = YELLOW_TANK_COLOR;
		} else if (tankName.equals(RED_TANK_NAME)) {
			color = RED_TANK_COLOR;
		}
		//
		return color;
	} // getTankRgbByName()

	/**
	 * @param tankName,
	 *            the tank name constant from IBlueTankSprite
	 * @return the tank selection screen image for the tank
	 */
	public static Image getTankSelectImageByName(String tankName) {
		//
		if (tankName == null)
			return null;
		//
		if (tankName.equals(BLUE_TANK_NAME)) {
			return getSelectBlue();
		} else if (tankName.equals(RED_TANK_NAME)) {
			return getSelectRed();
		} else if (tankName.equals(GREEN_TANK_NAME)) {
			return getSelectGreen();
		} else if (tankName.equals(YELLOW_TANK_NAME)) {
			return getSelectYellow();
		}
		return null;
	} // getTankSelectImageByName()

	/**
	 * @param tankName,
	 *            the tank name constant from IBlueTankSprite
	 * @return the tank firing image
	 */
	public static Image getTankFireImageByName(String tankName) {
		//
		if (tankName == null)
			return null;
		//
		if (tankName.equals(BLUE_TANK_NAME)) {
			return getFireBlue();
		} else if (tankName.equals(RED_TANK_NAME)) {
			return getFireRed();
		} else if (tankName.equals(GREEN_TANK_NAME)) {
			return getFireGreen();
		} else if (tankName.equals(YELLOW_TANK_NAME)) {
			return getFireYellow();
		}
		return null;
	} // getTankFireImageByName()

	/**
	 * @param tankName,
	 *            the tank name constant from IBlueTankSprite
	 * @return the normal tank image
	 */
	public static Image getTankImageByName(String tankName) {
		//
		if (tankName == null)
			return null;
		//
		if (tankName.equals(BLUE_TANK_NAME)) {
			return getBlueTank();
		} else if (tankName.equals(RED_TANK_NAME)) {
			return getRedTank();
		} else if (tankName.equals(GREEN_TANK_NAME)) {
			return getGreenTank();
		} else if (tankName.equals(YELLOW_TANK_NAME)) {
			return getYellowTank();
		}
		return null;
	} // getTankImageByName()

	/**
	 * @param tankName,
	 *            the tank name constant from IBlueTankSprite
	 * @return the blue tank selection screen image
	 */
	public static Image getSelectBlue() {
		try {
			if (selectBlue == null) {
				selectBlue = Image.createImage(BLUE_TANK_SELECT_IMG_SRC);
			}
		} catch (IOException e) {
		}
		return selectBlue;
	} // getSelectBlue()

	/**
	 * @param tankName,
	 *            the tank name constant from IBlueTankSprite
	 * @return the green tank selection screen image
	 */
	public static Image getSelectGreen() {
		try {
			if (selectGreen == null) {
				selectGreen = Image.createImage(GREEN_TANK_SELECT_IMG_SRC);
			}
		} catch (IOException e) {
		}
		return selectGreen;
	} // getSelectGreen()

	/**
	 * @param tankName,
	 *            the tank name constant from IBlueTankSprite
	 * @return the red tank selection screen image
	 */
	public static Image getSelectRed() {
		try {
			if (selectRed == null) {
				selectRed = Image.createImage(RED_TANK_SELECT_IMG_SRC);
			}
		} catch (IOException e) {
		}
		return selectRed;
	} // getSelectRed()

	/**
	 * @param tankName,
	 *            the tank name constant from IBlueTankSprite
	 * @return the yellow tank selection screen image
	 */
	public static Image getSelectYellow() {
		try {
			if (selectYellow == null) {
				selectYellow = Image.createImage(YELLOW_TANK_SELECT_IMG_SRC);
			}
		} catch (IOException e) {
		}
		return selectYellow;
	} // getSelectYellow()

	/**
	 * @param tankName,
	 *            the tank name constant from IBlueTankSprite
	 * @return the blue tank normal playfield image
	 */
	public static Image getBlueTank() {
		try {
			if (blueTank == null) {
				blueTank = Image.createImage(BLUE_TANK_IMG_SRC);
			}
		} catch (IOException e) {
		}
		return blueTank;
	} // getBlueTank()

	/**
	 * @param tankName,
	 *            the tank name constant from IBlueTankSprite
	 * @return the green tank normal playfield image
	 */
	public static Image getGreenTank() {
		try {
			if (greenTank == null) {
				greenTank = Image.createImage(GREEN_TANK_IMG_SRC);
			}
		} catch (IOException e) {
		}
		return greenTank;
	} // getGreenTank()

	/**
	 * @param tankName,
	 *            the tank name constant from IBlueTankSprite
	 * @return the red tank normal playfield image
	 */
	public static Image getRedTank() {
		try {
			if (redTank == null) {
				redTank = Image.createImage(RED_TANK_IMG_SRC);
			}
		} catch (IOException e) {
		}
		return redTank;
	} // getRedTank()

	/**
	 * @param tankName,
	 *            the tank name constant from IBlueTankSprite
	 * @return the yellow tank normal playfield image
	 */
	public static Image getYellowTank() {
		try {
			if (yellowTank == null) {
				yellowTank = Image.createImage(YELLOW_TANK_IMG_SRC);
			}
		} catch (IOException e) {
		}
		return yellowTank;
	} // getYellowTank()

	/**
	 * @param tankName,
	 *            the tank name constant from IBlueTankSprite
	 * @return the blue tank firing playfield image
	 */
	public static Image getFireBlue() {
		try {
			if (fireBlue == null) {
				fireBlue = Image.createImage(BLUE_TANK_FIRE_IMG_SRC);
			}
		} catch (IOException e) {
		}
		return fireBlue;
	} // getBlueTank()

	/**
	 * @param tankName,
	 *            the tank name constant from IBlueTankSprite
	 * @return the green tank firing playfield image
	 */
	public static Image getFireGreen() {
		try {
			if (fireGreen == null) {
				fireGreen = Image.createImage(GREEN_TANK_FIRE_IMG_SRC);
			}
		} catch (IOException e) {
		}
		return fireGreen;
	} // getGreenTank()

	/**
	 * @param tankName,
	 *            the tank name constant from IBlueTankSprite
	 * @return the red tank firing playfield image
	 */
	public static Image getFireRed() {
		try {
			if (fireRed == null) {
				fireRed = Image.createImage(RED_TANK_FIRE_IMG_SRC);
			}
		} catch (IOException e) {
		}
		return fireRed;
	} // getRedTank()

	/**
	 * @param tankName,
	 *            the tank name constant from IBlueTankSprite
	 * @return the yellow tank firing playfield image
	 */
	public static Image getFireYellow() {
		try {
			if (fireYellow == null) {
				fireYellow = Image.createImage(YELLOW_TANK_FIRE_IMG_SRC);
			}
		} catch (IOException e) {
		}
		return fireYellow;
	} // getYellowTank()

	/**
	 * @return the tank projectile playfield image
	 */
	public static Image getCannonBall() {
		try {
			if (cannonBall == null) {
				cannonBall = Image.createImage(CANNON_BALL_IMG_SRC);
			}
		} catch (IOException e) {
		}
		return cannonBall;
	} // getCannonBall()

	/**
	 * @return the destroyed tank first frame
	 */
	public static Image getDestroyedTankOne() {
		try {
			if (destroyedTankFrameOne == null) {
				destroyedTankFrameOne = Image
						.createImage(DESTROYED_TANK_ONE_IMG_SRC);
			}
		} catch (IOException e) {
		}
		return destroyedTankFrameOne;
	} // getDestroyedTankOne()

	/**
	 * @return the destroyed tank second frame
	 */
	public static Image getDestroyedTankTwo() {
		try {
			if (destroyedTankFrameTwo == null) {
				destroyedTankFrameTwo = Image
						.createImage(DESTROYED_TANK_TWO_IMG_SRC);
			}
		} catch (IOException e) {
		}
		return destroyedTankFrameTwo;
	} // getDestroyedTankTwo()

	/**
	 * @return the "BATTLE LOST" graphic
	 */
	public static Image getBattleLost() {
		try {
			if (battleLost == null) {
				battleLost = Image.createImage(BATTLE_LOST_IMG_SRC);
			}
		} catch (IOException e) {
		}
		return battleLost;
	} // getBattleLost()

	/**
	 * @return the "BATTLE WON" graphic
	 */
	public static Image getBattleWon() {
		try {
			if (battleWon == null) {
				battleWon = Image.createImage(BATTLE_WON_IMG_SRC);
			}
		} catch (IOException e) {
		}
		return battleWon;
	} // getBattleWon()
	
	
	/**
	 * @return the "BATTLE DRAW" graphic
	 */
	public static Image getBattleDraw() {
		try {
			if (battleDraw == null) {
				battleDraw = Image.createImage(BATTLE_DRAW_IMG_SRC);
			}
		} catch (IOException e) {
		}
		return battleDraw;
	} // getBattleDraw()

	/**
	 * @return the destroyed tank third frame
	 */
	public static Image getDestroyedTankThree() {
		try {
			if (destroyedTankFrameThree == null) {
				destroyedTankFrameThree = Image
						.createImage(DESTROYED_TANK_THREE_IMG_SRC);
			}
		} catch (IOException e) {
		}
		return destroyedTankFrameThree;
	} // getDestroyedTankThree()

	/**
	 * This cached version of this image will be recreated if the dimensions of
	 * the screen change
	 * 
	 * @return the game background image
	 */
	public static Image getCamouflage(int width, int height) {
		try {
			if (camouflage == null || camouflageWidth != width
					|| camouflageHeight != height) {
				camouflage = Image.createImage(Image
						.createImage(CAMOUFLAGE_IMG_SRC), 0, 0, width, height,
						Sprite.TRANS_NONE);
				//
				camouflageWidth = width;
				camouflageHeight = height;
			}
		} catch (IOException e) {
		}
		return camouflage;
	} // getCamouflage()

	/**
	 */
	public void update() {
		// not implemented
	} // update()

} // BlueTankImageHolder()
